/*
 * Copyright 2006 Martin Konzett <gekomic AT gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.toools.gwt.wmc.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import com.google.gwt.user.client.ui.Widget;
import org.toools.gwt.widget.client.NavigationPanel;
import org.toools.gwt.wmc.client.account.gui.AccountPanel;
import org.toools.gwt.wmc.client.additive.AdditiveService;
import org.toools.gwt.wmc.client.additive.gui.AdditiveControl;
import org.toools.gwt.wmc.client.application.ApplicationService;
import org.toools.gwt.wmc.client.application.gui.ApplicationControl;
import org.toools.gwt.wmc.client.company.CompanyService;
import org.toools.gwt.wmc.client.company.gui.CompanyControl;
import org.toools.gwt.wmc.client.companypart.CompanyPartService;
import org.toools.gwt.wmc.client.companypart.gui.CompanyPartControl;
import org.toools.gwt.wmc.client.container.ContainerService;
import org.toools.gwt.wmc.client.container.gui.ContainerControl;
import org.toools.gwt.wmc.client.disposer.DisposerService;
import org.toools.gwt.wmc.client.disposer.gui.DisposerControl;
import org.toools.gwt.wmc.client.waste.WasteService;
import org.toools.gwt.wmc.client.waste.gui.WasteControl;

public class WMCEntryPoint implements EntryPoint, SimpleActionListener {

    private final VerticalPanel vp = new VerticalPanel();

    FlowPanel main = new FlowPanel();

    Widget active = new Label("");

    VerticalPanel headerPanel = new VerticalPanel();
    HorizontalPanel headerInnerPanel = new HorizontalPanel();
    HorizontalPanel headerLeftPanel = new HorizontalPanel();
    VerticalPanel activePanel = new VerticalPanel();
    VerticalPanel footerPanel = new VerticalPanel();

    //HTML logo = new HTML("<img src='logo.jpg'>");

    public void onModuleLoad() {

        // setup layout
        main.setStyleName("main");
        headerPanel.setStyleName("header");
        headerInnerPanel.setStyleName("headerInner");
        headerLeftPanel.setStyleName("headerLeft");
        activePanel.setStyleName("active");
        footerPanel.setStyleName("footer");
        //logo.setStyleName("logo");

        headerLeftPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);

        HorizontalPanel p1 = new HorizontalPanel();
        //p1.add(logo);
        headerLeftPanel.add(p1);

        headerInnerPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
        headerInnerPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
        headerInnerPanel.add(headerLeftPanel);
        headerInnerPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
        headerInnerPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        headerPanel.add(headerInnerPanel);
        headerPanel.add(new HTML("<hr/>"));
        activePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        activePanel.add(active);
        footerPanel.add(new HTML("<hr/>"));
        footerPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        footerPanel.add(new HTML("&copy <a href=\"http://www.toools.org\">toools.org</a>"));

        main.add(headerPanel);
        main.add(activePanel);
        main.add(footerPanel);

        new AccountCommand().execute();
        RootPanel.get().add(main);
        RootPanel.get().add(vp);
    }

    /*
     * navigation setup
     */
    private NavigationPanel createNavigation() {
        return new NavigationPanel(
                new String[][] {{"Company", "Companies"}, {"Disposer", "Disposers"},{"Logout","Do"}},
                new Command[][] {{new CompanyCommand(), new CompanyCommand()}, {new DisposerCommand(), new DisposerCommand() }, {new LogoutCommand(), new LogoutCommand()}});
    }

    private void activate(Widget w) {
        activePanel.remove(active);
        active = w;
        activePanel.add(active);
    }

    //Login
    public void actionPerformed( SimpleActionEvent ae ) {
        HorizontalPanel hp = new HorizontalPanel();
        hp.add( createNavigation() );
        headerPanel.add( hp );
        new CompanyCommand().execute();
    }
    
    /**
     * commands
     */
    private class AccountCommand implements Command {
        public void execute() {
            long now = new java.util.Date().getTime();
            AccountPanel acp = new AccountPanel();
            acp.addSimpleActionListener( WMCEntryPoint.this );
            activate( acp );
            System.out.println("--> command took: " + (new java.util.Date().getTime() - now) + "ms");
        }
    }
    /**
     * commands
     */
    private class LogoutCommand implements Command {
        public void execute() {

            new AccountCommand().execute();
        }
    }

    private class CompanyCommand implements Command {
        public void execute() {
            long now = new java.util.Date().getTime();
            activate(new CompanyControl(CompanyService.Util.getInstance()));
            System.out.println("--> command took: " + (new java.util.Date().getTime() - now) + "ms");
        }
    }

    private class DisposerCommand implements Command {
        public void execute() {
            long now = new java.util.Date().getTime();
            activate(new DisposerControl(DisposerService.Util.getInstance()));
            System.out.println("--> command took: " + (new java.util.Date().getTime() - now) + "ms");
        }
    }
}